package com.lyc.service.impl;

import com.lyc.constant.Constant;
import com.lyc.dao.ISolrDao;
import com.lyc.entity.Product;
import com.lyc.entity.qo.Conditions;
import com.lyc.service.ISolrService;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.common.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@Service
public class SolrServiceImpl implements ISolrService {

    @Resource(name = "solrDao")
    private ISolrDao solrDao;

    @Override
    public void test() throws IOException, SolrServerException {
        solrDao.test();
    }

    @Override
    public List<Product> searchProducts(Conditions conditions) throws IOException, SolrServerException {
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.set("df", "product_name");
        solrQuery.setQuery("*");
        //TODO 设置分页
        solrQuery.setStart(Constant.QUERY_START);
        solrQuery.setRows(Constant.PRODUCT_PAGE_SIZE);
        if (conditions != null) {
            //设置排序
            Integer sort = conditions.getSort();
            if (sort != null) {
                switch (sort) {
                    case Constant.PRICE_ASC:
                        solrQuery.setSort("product_off_price", SolrQuery.ORDER.asc);
                        break;
                    case Constant.PRICE_DESC:
                        solrQuery.setSort("product_off_price", SolrQuery.ORDER.desc);
                        break;
                    case Constant.SALE_DESC:
                        solrQuery.setSort("product_sale", SolrQuery.ORDER.desc);
                        break;
                    case Constant.SALE_ASC:
                        solrQuery.setSort("product_sale", SolrQuery.ORDER.asc);
                        break;
                }
            }
            //价格区间搜索
            if (conditions.getStartPrice() != null && conditions.getEndPrice() != null) {
                solrQuery.addFilterQuery("product_off_price:[" + conditions.getStartPrice() + " TO " + conditions.getEndPrice() + " ]");
            }
            //设置过滤条件
            Integer cid = conditions.getCid();
            if (cid != null)
                solrQuery.addFilterQuery("product_category_id:" + cid);
            //搜索关键字并高亮显示
            String keyword = conditions.getKeyword();
            if (!StringUtils.isEmpty(keyword)) {
                solrQuery.setQuery(keyword);
                solrQuery.setHighlight(true);
                solrQuery.addHighlightField("product_name");
                solrQuery.setHighlightSimplePre("<span style=\"color:red\">");
                solrQuery.setHighlightSimplePost("</span>");
            }
        }
        return solrDao.queryProducts(solrQuery);
    }

    @Override
    public List<Product> getNewProducts() throws IOException, SolrServerException {
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.setQuery("*:*");
        //查询前12条记录
        solrQuery.setStart(Constant.QUERY_START);
        solrQuery.setRows(Constant.QUERY_COUNT);
        //查询最新商品
        solrQuery.setSort("product_date", SolrQuery.ORDER.desc);
        return solrDao.queryProducts(solrQuery);
    }

    @Override
    public List<Product> getHotProducts() throws IOException, SolrServerException {
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.setQuery("*:*");
        solrQuery.setStart(Constant.QUERY_START);
        solrQuery.setRows(Constant.QUERY_COUNT);
        //根据销售数量降序排序
        solrQuery.setSort("product_sale", SolrQuery.ORDER.desc);
        return solrDao.queryProducts(solrQuery);
    }
}
